package com.sh.healthtv.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;

import com.sh.healthtv.R;
import com.sh.healthtv.app.AppManager;

/**
 * Created by zhouyucheng on 2017/8/4.
 */

public class PlayerActivity extends FragmentActivity {

    private  String TAG="PA";
    private EpgPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.player);
        AppManager.getAppManager().addActivity(this);
        getIntentDate();
    }

    // 收藏进来时自己判断
    private void getIntentDate() {
        Bundle bundle = getIntent().getExtras();
        String videoId = "";
        String programName = "";
        String  progress="";
        String isConnect="";
        if (bundle != null) {
            videoId = bundle.getString("programId", "");
            programName = bundle.getString("programName", "");
            progress=bundle.getString("progress","-1");
            isConnect=bundle.getString("isConnect","0");
            Log.i(TAG, "getIntentDate:=========== ");
            Log.i(TAG,videoId +"/"+programName+"/"+ progress+"/"+ isConnect);
        }
        if (player == null) {
            player = new EpgPlayer();
            addFragment(player, R.id.epgcontroller);
            player.readyPlayEpgSingle(videoId, programName,progress,isConnect);
        } else {
            player.readyPlayEpgSingle(videoId, programName,progress,isConnect);
            player.play(videoId, programName);
        }
    }


    public void addFragment(Fragment fragment, int contain) {
        FragmentManager fM = getSupportFragmentManager();
        FragmentTransaction beginTransaction = fM.beginTransaction();
        beginTransaction.replace(contain, fragment);
        beginTransaction.commitAllowingStateLoss();
    }

    ;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Boolean call = true;
        if (player != null) {
            call = player.onKeyDown(keyCode, event);
        }
        return call;
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
}
